package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.OrderInfo;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MemberService;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequestMapping("/mobile")
@RestController
public class MobileCotroller {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private MemberService memberService;
    @PostMapping("/validateCode/send")
    private Result mobile(User user, String type, HttpSession session){
        log.info("前后端联通");
        //获取用户手机号码
        String phone = user.getTelephone();
        //生成随机数
        String code = ValidateCodeUtils.generateValidateCode(4).toString();
        log.info("验证码:=======>{}",code);
        //写入到session中
        session.setAttribute(phone,code);

        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }
    @PostMapping("/login/smsLogin")
    private Result login(@RequestBody Map map, HttpSession session, HttpServletResponse response){
        log.info("前后端连通==={}",map);
        String phone = map.get("telephone").toString();
        String newCode = map.get("validateCode").toString();
        String oldCode = (String) session.getAttribute(phone);

        if (!newCode.equals(oldCode)&& oldCode.equals(oldCode)){
            return new Result(false,MessageConst.LOGIN_FAIL);
        }
        //检查用户是否存在
        LambdaQueryWrapper<Member> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Member::getPhoneNumber,phone);
        Member member = memberService.findByTelephone(phone);
        if (member==null){
            member = new Member();
            member.setPhoneNumber(phone);
            member.setRegTime(new Date());
            this.memberService.add(member);
        }
        Cookie cookie = new Cookie("login_member_telephone",phone);
        cookie.setPath("/");
        cookie.setMaxAge(60*60*24*30);
        response.addCookie(cookie);

        session.setAttribute(MessageConst.LOGIN_SUCCESS,member.getId());
        return new Result(true,MessageConst.LOGIN_SUCCESS,member);
    }
    @GetMapping("/setmeal/getSetmeal")
    public Result getSetmeal(){
        try {
            List<Setmeal> setmealList = memberService.getSetmeal();
            return new Result(true,MessageConst.ACTION_SUCCESS,setmealList);
        } catch (Exception e) {
            return new Result(false,MessageConst.ACTION_FAIL);
        }

    }
    @GetMapping("/setmeal/findById")
    public Result findById(Integer id){
        log.info("前后端连通");
        try {
            Setmeal setmeal = memberService.findById(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,setmeal);
        } catch (Exception e) {
            return new Result(false,MessageConst.ACTION_FAIL);
        }

    }
    @GetMapping("/order/findById")
    public Result findOrderById(Integer id) {
        log.info("前后端连通");
        try {
            OrderInfo orderInfo = memberService.findOrderById(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,orderInfo);
        } catch (Exception e) {
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
}

